package com.example.springbott.service;

import com.example.springbott.bean.MarketStorage;
import com.example.springbott.bean.MarketStorageExample;
import com.example.springbott.bean.common.CommonData;
import com.example.springbott.bean.common.PageInfoBase;
import com.example.springbott.mapper.MarketStorageMapper;
import com.example.springbott.util.EazyExampleSetUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.SneakyThrows;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.util.Date;
import java.util.List;
import java.util.UUID;

@Component
public class adminStorageServiceImpl implements adminStorageService {
    @Autowired
    MarketStorageMapper marketStorageMapper;

    @SneakyThrows
    @Override
    public CommonData adminStorageList(PageInfoBase pageInfoBase, String key, String name) {

        PageHelper.startPage(pageInfoBase.getPage(),pageInfoBase.getLimit());
        MarketStorageExample marketStorageExample = new MarketStorageExample();
        EazyExampleSetUtil.setPageAndCondition(marketStorageExample, pageInfoBase);
        MarketStorageExample.Criteria criteria = marketStorageExample.createCriteria();
        if (key != null) {
            criteria.andKeyEqualTo(key);
        } else if (name != null) {
            criteria.andNameEqualTo(name);
        }
        criteria.andDeletedEqualTo(false);
        List<MarketStorage> marketStorages = marketStorageMapper.selectByExample(marketStorageExample);
        PageInfo<MarketStorage> marketStoragePageInfo = new PageInfo<>(marketStorages);
        return CommonData.data(marketStoragePageInfo);

    }

    @Override
    @Transactional
    public MarketStorage adminStorageCreate(HttpServletRequest request, MultipartFile file) throws IOException {
        File saveFile = new File("C:/spring", file.getOriginalFilename());
        file.transferTo(saveFile);
        MarketStorage marketStorage = new MarketStorage();
        marketStorage.setAddTime(new Date());
        System.out.println(file.getOriginalFilename());
        String[] split = file.getOriginalFilename().split("\\.");


        String key = UUID.randomUUID().toString() + "." + split[1];
        marketStorage.setKey(key);
        marketStorage.setName(file.getOriginalFilename());
        marketStorage.setType(file.getContentType());
        marketStorage.setSize((int) file.getSize());
        marketStorage.setUrl("http://localhost:8083" + "/springStatic/" + file.getOriginalFilename());
        marketStorage.setAddTime(new Date());
        marketStorage.setUpdateTime(null);
        marketStorage.setDeleted(false);
        marketStorageMapper.insert(marketStorage);
        return marketStorage;


    }

    @Override
    @Transactional
    public MarketStorage adminStorageUpdate(MarketStorage marketStorage) {
        MarketStorageExample marketStorageExample = new MarketStorageExample();
        marketStorage.setUpdateTime(new Date());
        marketStorage.setName(marketStorage.getName());
        marketStorageMapper.updateByExample(marketStorage,marketStorageExample);
       return marketStorage;

    }

    @Override
    @Transactional
    public Integer adminStorageDelete(MarketStorage marketStorage) {
        MarketStorageExample marketStorageExample = new MarketStorageExample();
        marketStorage.setDeleted(true);
        MarketStorageExample.Criteria criteria = marketStorageExample.createCriteria();
        criteria.andIdEqualTo(marketStorage.getId());
        return marketStorageMapper.updateByExample(marketStorage,marketStorageExample);

    }


}
